<?php

namespace app\backend\controller;

use app\common\controller\Basic;
use app\common\service\crm\BusinessService;
use app\common\service\crm\ContactsService;
use app\common\service\hrm\CompanyService;
use app\common\service\hrm\DeptService;
use app\common\service\hrm\RankingService;
use app\common\service\sys\DictService;
use app\common\service\sys\UserGroupService;
use app\common\service\sys\UserService;
use app\common\service\xzm\CarService;
use app\common\service\sys\UserGroupMemberService;
use app\common\service\pmm\AuditGroupService;
use app\common\service\crm\CustomerService;
use app\common\service\pmm\ConfigService;
use app\common\service\sys\MenuService;
use app\common\service\wms\CategoryService;
use app\common\service\wms\ProductService;
use app\common\service\wms\WarehouseService;
use app\common\service\xzm\GoodsTypeService;
use app\common\service\xzm\PropertyTypeService;
use app\common\service\xzm\BooksTypeService;
use app\common\service\sys\FolderTypeService;

/**
 * 弹出选择窗集聚在此控制器|公共使用的也放此
 * @author kaige
 * @update 2022-04-29
 */
class Select extends Basic
{

    //选择文件夹树型
    public function foldertypetree()
    {
        if ($this->request->isGet()) {
            return _jok('', (new FolderTypeService())->typeTree());
        }
        return _jerr(1000);
    }
    //选择图书分类树型
    public function bookstypetree()
    {
        if ($this->request->isGet()) {
            return _jok('', (new BooksTypeService())->booksTypeTree());
        }
        return _jerr(1000);
    }

    //选择资产分类树型
    public function propertypetree()
    {
        if ($this->request->isGet()) {
            return _jok('', (new PropertyTypeService())->propertytypeTree());
        }
        return _jerr(1000);
    }


    //选择物品分类树型
    public function goodstypetree()
    {
        if ($this->request->isGet()) {
            return _jok('', (new GoodsTypeService())->goodstypeTree());
        }
        return _jerr(1000);
    }

    //用户ids 取出用户列表
    public function useridslist()
    {
        if ($this->request->isGet()) {
            $ids = input('ids/s', '');
            $ids = urldecode($ids);
            if ($ids) {
                $ids = explode(',', $ids);
                $list = (new UserService())->getListAll([
                    ['id', 'IN', $ids],
                    ['status', '=', 1],
                ], [], ['id,real_name as name,dept_id_name,ranking_id_name,sex']);
                if ($list) {
                    return _jok('', $list);
                }
            }
            return _jerr(1034);
        }
        return _jerr(1000);
    }
    //公共信息集
    public function getorigingdept()
    {
        //当前用户、属于X单位X部门对应的根id
        if ($this->request->isGet()) {
            $company_id = env('COMPANY_ID', 11);
            $info = (new DeptService())->getInfoByWhere([
                ['company_id', '=', $company_id],
                ['pid', '=', 0],
            ], ['id,name']);
            if ($info) {
                return _jok('', $info);
            }
            return _jerr(1034);
        }
        return _jerr(1000);
    }

    //选择单位
    public function company()
    {
        if ($this->request->isGet()) {
            return json((new CompanyService())->_list(input('param.')));
        }
        return _jerr(1000);
    }

    //选择单位树型
    public function companytree()
    {
        if ($this->request->isGet()) {
            return _jok('', (new CompanyService())->companyTree());
        }
        return _jerr(1000);
    }

    //获取流程配置--单模块
    public function flowconfig()
    {
        if ($this->request->isGet()) {
            $modu_id = input('modu_id/d', 0);
            $outlist = (new ConfigService())->getInfoById($modu_id);
            //手动模式，删除表单配置
            if ($outlist['form_type'] == 2) {
                unset($outlist['form_json']);
            }
            return json($outlist);
        }
        return _jerr(1000);
    }

    //选择模块----需要缓存
    public function selectmodu()
    {
        if ($this->request->isGet()) {
            if (cache('select_config_list')) {
                return json(cache('select_config_list'));
            } else {
                $data = input('param.');
                $data['field'] = 'id,name,flow_type';
                $result = (new ConfigService())->_list($data);
                cache('select_config_list', $result, 86400); //默认一天
                return json($result);
            }
        }
        return _jerr(1000);
    }

    //选择员工
    public function employee()
    {
        if ($this->request->isGet()) {
            $data = input('param.');
            url_decode($data);
            return json((new UserService())->_list($data));
        }
        return _jerr(1000);
    }

    //选择职位
    public function ranking()
    {
        if ($this->request->isGet()) {
            return (new RankingService())->_list(input('param.'));
        }
        return _jerr(1000);
    }
    //选择职位树型
    public function rankingtree()
    {
        if ($this->request->isGet()) {
            return _jok('', (new RankingService())->rankingTree());
        }
        return _jerr(1000);
    }

    //选择部门
    public function dept()
    {
        if ($this->request->isGet()) {
            return (new DeptService())->_list(input('param.'), false);
        }
        return _jerr(1000);
    }

    //选择部门树型
    public function depttree()
    {
        if ($this->request->isGet()) {
            return _jok('', (new DeptService())->deptTree());
        }
        return _jerr(1000);
    }

    //选择菜单树型
    public function menutree()
    {
        if ($this->request->isGet()) {
            return _jok('', (new MenuService())->menuTree());
        }
        return _jerr(1000);
    }


    //选择字典
    public function dict()
    {
        if ($this->request->isGet()) {
            return json((new DictService())->_list(input('param.'), false));
        }
        return _jerr(1000);
    }

    //选择字典树
    public function dicttree()
    {
        if ($this->request->isGet()) {
            return json((new DictService())->getDictTree());
        }
        return _jerr(1000);
    }

    // 选择用户分组
    public function usergroup()
    {
        if ($this->request->isGet()) {
            return json((new UserGroupService())->_list(input('param.'), false));
        }
        return _jerr(1000);
    }

    // 选择用户分组
    public function usergroupdept()
    {
        if ($this->request->isGet()) {
            return json([
                'group' => (new UserGroupService())->getUserGroupLayuiTree(),
                'dept' => (new DeptService())->getDeptLayuiTree()
            ]);
        }
        return _jerr(1000);
    }

    // 选择分组用户
    public function usergrouplist()
    {
        if ($this->request->isGet()) {
            $group_id = input('group_id/d', 0);
            return json((new UserGroupMemberService())->getUserByGroup($group_id));
        }
        return _jerr(1000);
    }

    // 选择用户
    public function ugdcuserlist()
    {
        if ($this->request->isGet()) {
            $ids = input('ids', '');
            return json((new UserService())->getugdcUserList($ids));
        }
        return _jerr(1000);
    }


    //选择车牌号
    public function licenseplatenumber()
    {
        if ($this->request->isGet()) {
            return json((new CarService())->_list(input('param.')));
        }
        return _jerr(1000);
    }

    //审核人员分组
    public function auditgroup()
    {
        if ($this->request->isGet()) {
            return (new AuditGroupService())->getauditgroup();
        }
        return _jerr(1000);
    }

    /**
     * 客户
     */
    public function customer()
    {
        if ($this->request->isGet()) {
            $data = input('param.');
            return json((new CustomerService())->_list($data));
        }
        return _jerr(1000);
    }

    /**
     * 产品类型
     */
    public function category()
    {
        if ($this->request->isGet()) {
            $data = input('param.');
            return json((new CategoryService())->_list($data));
        }
        return _jerr(1000);
    }

    /**
     * 产品类型树
     */
    public function categorytree()
    {
        if ($this->request->isGet()) {
            return _jok('', (new CategoryService())->categoryTree());
        }
        return _jerr(1000);
    }

    /**
     * 仓库区树
     */
    public function warehousetree()
    {
        if ($this->request->isGet()) {
            return _jok('', (new WarehouseService())->warehousetree());
        }
        return _jerr(1000);
    }

    /**
     * 产品
     */
    public function productlist()
    {
        if ($this->request->isGet()) {
            $data = input('param.');
            return json((new ProductService())->_list($data));
        }
        return _jerr(1000);
    }

    /**
     * 商机
     */
    public function business()
    {
        if ($this->request->isGet()) {
            $data = input('param.');
            return json((new BusinessService())->_list($data));
        }
        return _jerr(1000);
    }

    /**
     * 客户联系人
     */
    public function contacts()
    {
        if ($this->request->isGet()) {
            $data = input('param.');
            return json((new ContactsService())->_list($data));
        }
        return _jerr(1000);
    }
}
